package com.rfchina.okr.xianren.task;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.rfchina.okr.xianren.manager.MyMoneyManager;
import com.rfchina.okr.xianren.model.RechargeInfo;
import com.rfchina.okr.xianren.model.TaskAccepter;
import com.rfchina.okr.xianren.model.TaskInfo;
import com.rfchina.okr.xianren.model.WithdrawInfo;
import com.rfchina.okr.xianren.util.MoneyObjectType;

public class FinishTaskInfo implements Runnable {

	private static Logger logger = LoggerFactory.getLogger(FinishTaskInfo.class);

	@Override
	public void run() {

		String sql = "select * from task_info where status=10 and finish_status=2";
		List<TaskInfo> list = TaskInfo.dao.find(sql);
		for (TaskInfo info : list) {
			Date lockTime = new Date();

			String sql1 = "select * from task_accepter  where status=10 and task_id=? ";
			List<TaskAccepter> list1 = TaskAccepter.dao.find(sql1, info.getId());
			if (list1.isEmpty()) {
				logger.error("finish task:{}", info.getId());

				continue;
			}
			for (TaskAccepter accepter : list1) {
				String billAuditComment = "完成" + info.getId() + "任务:" + accepter.getId();
				int moneyFlowId = MyMoneyManager.getInstance().changeMoney(lockTime, accepter.getUid(),
						accepter.getId(), accepter.getAppId(), info.getSingleAmount(), billAuditComment,
						MoneyObjectType.task_accepter_finish);
				accepter.setMoneyFlowId(moneyFlowId);
				accepter.setSingleAmount(info.getSingleAmount());
				accepter.update();
			}
			info.setFinishStatus(3);
			info.update();
		}

		// sql = "select * from task_accepter where status=1 order by id limit 1";
		// List<WithdrawInfo> list1 = WithdrawInfo.dao.find(sql);
		// for (WithdrawInfo info : list1) {
		// Date lockTime = new Date();
		// Long uid = info.getUid();
		// Integer id = info.getId();
		// String appid = info.getAppid();
		// BigDecimal money = info.getAmount().multiply(BigDecimal.valueOf(-1L));
		// String billAuditComment = "定时任务提现";
		// int flow = MyMoneyManager.getInstance().changeMoney(lockTime, uid, id, appid,
		// money, billAuditComment,
		// MoneyObjectType.withdraw);
		// logger.info("flow:{},info:{},lockTime{}", flow, info, lockTime);
		// }

	}

}
